package pers.chao.cacheable.core;

import pers.chao.cacheable.core.annotation.CacheValue;
import pers.chao.cacheable.infrastructure.utils.RedisOp;

/**
 * Description: string缓存操作
 *
 * @author W.Chao
 * @date 2020/10/2 14:34
 **/
public class CacheValueProcessor extends AbstractCacheProcessor {
	private CacheValue annotation;

	public CacheValueProcessor(CacheValue annotation) {
		this.annotation = annotation;
	}

	@Override
	protected void preInfo() {
		logger.info("[Cache value previous INFO] @cache -> {}, @cacheValue -> {}", cacheConditionInfo(), annotation);
	}

	@Override
	public void parseRedisStructKeyOrOther() {

	}

	@Override
	public Object preQuery() {
		if (RedisOp.hasKey(finalKey)){
			return RedisOp.get(finalKey);
		}
		return null;
	}

	@Override
	public void afterMethodOperate(Object o) {
		if (evict) {
			// 清除
			RedisOp.del(finalKey);
			return;
		}

		boolean isFirst = !RedisOp.hasKey(finalKey);

		// 缓存期限
		if (isFirst && isNeedExpire) {
			RedisOp.set(finalKey, o, expire, unit);
		}else {
			RedisOp.set(finalKey, o);
		}
	}
}
